﻿/* 自动生成版本代码 */
const fs = require('fs');
const babel = require("@babel/core");
const types = require('@babel/types');
const template = require('@babel/template');
const moment = require('moment');

function BuildVersionCode(options) {
  this.options = options;
}
module.exports = BuildVersionCode;

BuildVersionCode.prototype.apply = function(compiler) {
  compiler.hooks.run.tapAsync('_', (compiler, callback) => {
    let program = types.program([
      template.statement('export let buildTime = (BUILD_TIME)')({
        BUILD_TIME: types.stringLiteral(moment().format('YYYYMMDDHHmm'))
      })
    ]);
  
    fs.writeFile(`src/${this.options.dir}/build-version.js`, babel.transformFromAstSync(program).code, callback);
  });
};